/*
 *
 *      Copyright (c) 2018-2025, st All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the pig4cloud.com developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: st
 *
 */

package com.mkh.st.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mkh.st.admin.api.dto.*;
import com.mkh.st.admin.api.vo.*;
import com.mkh.st.admin.service.SysUserService;
import com.mkh.st.common.core.util.R;
import com.mkh.st.common.data.datascope.RedisUtil;
import com.mkh.st.common.log.annotation.SysLog;
import com.mkh.st.common.security.annotation.Inner;
import com.mkh.st.purchase.vo.PurchaseGroupVO;
import com.mkh.st.purchase.vo.SupplierVO;
import com.mkh.st.warehouse.vo.WarehouseFeignVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author st
 * @date 2018/12/16
 */
@RestController
@AllArgsConstructor
@RequestMapping("/user")
@Api(value = "user", tags = "用户管理模块")
public class SysUserController {

	private final SysUserService userService;

	/**
	 * 分页查询用户
	 * @param page 参数集
	 * @param dto 查询参数列表
	 * @return 用户集合
	 */
	@GetMapping("/getUserPage")
	@ApiOperation(value = "用户管理分页查询", notes = "账户管理分页查询")
	public R<IPage<UserPageVO>> getUserPage(Page page, UserPageDTO dto) {
		return userService.getUserPage(page, dto);
	}

	@GetMapping("/getUserList")
	@ApiOperation(value = "复制权限选择用户查询", notes = "复制权限选择用户查询")
	public R<List<UserPageVO>> getUserList(UserPageDTO dto) {
		return R.ok(userService.getUserList(dto));
	}

	@GetMapping("/getUserDetail")
	@ApiOperation(value = "用户详情查询", notes = "用户详情查询")
	public R<UserDetailVO> getUserDetail(@RequestParam Long userId) {
		return userService.getUserDetail(userId);
	}

	@PostMapping("/saveUserInfo")
	@ApiOperation(value = "用户信息保存", notes = "用户信息保存")
	public R<Boolean> saveUserInfo(@Valid @RequestBody UserSaveDTO dto) {
		return R.ok(userService.saveUserInfo(dto));
	}

	@PostMapping("/updateUserPassword")
	@ApiOperation(value = "密码修改", notes = "密码修改")
	public R<Boolean> updateUserPassword(@Valid @RequestBody UserPasswordDTO dto) {
		return R.ok(userService.updateUserPassword(dto));
	}

	@PostMapping("/copyUserDataScope")
	@ApiOperation(value = "复制权限", notes = "复制权限")
	public R<Boolean> copyUserDataScope(@Valid @RequestBody UserCopyDataScopeDTO dto) {
		return R.ok(userService.copyUserDataScope(dto));
	}

	@GetMapping("/getUserRoleScopeList")
	@ApiOperation(value = "数据权限分配角色查询", notes = "数据权限分配角色查询")
	public R<List<UserRoleScopeListVO>> getUserRoleScopeList(@Valid UserRoleScopeDTO dto) {
		return R.ok(userService.getUserRoleScopeList(dto));
	}

	@GetMapping("/getUserScopeDataList")
	@ApiOperation(value = "数据权限分配详情查询", notes = "数据权限分配详情查询")
	public R<List<ScopeDataDeatil>> getUserScopeDataList(@Valid UserRoleDataDTO dto) {
		return R.ok(userService.getUserScopeDataList(dto));
	}

	@PostMapping("/userScopeDataSave")
	@ApiOperation(value = "数据权限分配保存", notes = "数据权限分配保存")
	public R<Boolean> userScopeDataSave(@Valid @RequestBody UserRoleDataSaveDTO dto) {
		return R.ok(userService.userScopeDataSave(dto));
	}

	@GetMapping("/getPurchaseUserList")
	@ApiOperation(value = "现场采购单获取采购员列表", notes = "现场采购单获取采购员列表")
	public R<List<UserIdNameVO>> getPurchaseUserList(@ApiParam(name = "keyWord", value = "采购员名查询") String keyWord) {
		return R.ok(userService.getPurchaseUserList(keyWord));
	}

	@GetMapping("/getUsersList")
	@ApiOperation(value = "采购员,销售,财务人员列表", notes = "采购员,销售,财务人员列表")
	public R<List<UserIdNameVO>> getUsersList(@ApiParam(name = "keyWord", value = "人员名查询") String keyWord,
											  @ApiParam(name = "userType", value = "人员类型:1采购员,2销售,3财务人员",required = true) Integer userType,
											  @ApiParam(name = "menuCode", value = "菜单code",required = true) String menuCode
											  ) {
		return R.ok(userService.getUsersList(keyWord,userType,menuCode));
	}

	@GetMapping("/getAllUsersList")
	@ApiOperation(value = "全部人员列表", notes = "全部人员列表")
	public R<List<UserIdNameVO>> getAllUsersList(@ApiParam(name = "keyWord", value = "人员名查询") String keyWord,
											  @ApiParam(name = "menuCode", value = "菜单code",required = true) String menuCode) {
		return R.ok(userService.getAllUsersList(keyWord,menuCode));
	}

	@GetMapping("/getPurchaseGroupList")
	@ApiOperation(value = "采购组列表", notes = "采购组列表")
	public R<List<PurchaseGroupVO>> getPurchaseGroupList(@ApiParam(name = "menuCode", value = "菜单code",required = true) String menuCode,
														 @ApiParam(name = "siteId", value = "业务线id") Long siteId) {
		return R.ok(userService.getPurchaseGroupList(menuCode,siteId));
	}

	@GetMapping("/getSupplierList")
	@ApiOperation(value = "供应商列表", notes = "供应商列表")
	public R<List<SupplierVO>> getSupplierList(@ApiParam(name = "menuCode", value = "菜单code",required = true) String menuCode) {
		return R.ok(userService.getSupplierList(menuCode));
	}

	@GetMapping("/getPermissionList")
	@ApiOperation(value = "获取用户按钮", notes = "获取用户按钮")
	public R<List<String>> getPermissionList() {
		return R.ok(userService.getPermissionList());
	}

	@GetMapping("/getUserDetailScope")
	@ApiOperation(value = "查询用户详情", notes = "查询用户详情")
	public R<UserDetailVO> getUserDetailScope(@RequestParam Long userId) {
		return R.ok(userService.getUserDetailScope(userId));
	}
}
